// index.ts
// 获取应用实例
const drawQrcode = require('../../utils/weapp.qrcode.min.js')

Component({
  data: {
    isOrder: false,
    infoData: {
      title: "新年盲盒抽奖",
      status: 'close',
      shareFace: "",
      context: [
        "活动时间：即日起至2024年12月31日22:00",
        "活动说明：一个手机号仅可参与一次惊喜盲盒抽奖机会，奖品数量有限，抽完即止；奖券需先至武林银泰A馆7F客服中心核销并兑换成纸质券后才可使用",
        "兑换时间：即日起至2024年12月31日22:00",
        "兑换地点：武林银泰A馆7楼客服中心",
      ]
    },
    awared: {
      isAward: false,
      name: "",
      _id: "",
    },
    userInfo: {
      _id: "",
      isGet: false,
      hasAward: "pedding",
    },
    isShow: false,
    shareData: {
      title: '银泰新年盲盒抽奖',
      path: '/pages/index/index',
    }
  },
  methods: {
    onPullDownRefresh() {
      this.log();
      wx.stopPullDownRefresh()
    },
    onLoad() {
      this.info();
      this.log();
      if (wx.getStorageSync("password")) {
        this.setData({
          isOrder: true
        })
      } else {
        this.setData({
          isOrder: false
        })
      }
    },
    hanldeInfo() {
      const { infoData } = this.data;
      if (infoData.status !== 'close') {
        this.buy()
      } else {
        wx.showToast({
          icon: 'none',
          title: '活动未开始'
        })
      }
    },
    hanldeClose() {
      this.setData({
        isShow: false
      })
      this.onLoad()
    },
    hanldeOrder() {
      wx.navigateTo({
        url: '/pages/order/index'
      })
    },
    onShareAppMessage() {
      return {
        ...this.data.shareData,
        imageUrl: this.data.infoData.shareFace,
        title: this.data.infoData.title
      };
    },
    log() {
      wx.showLoading({
        title: ""
      })
      const that = this
      wx.cloud.callFunction({
        name: 'activelog',
        data: { action: 'log', params: {  } },
        success({result}: any) {
          that.setData({
            userInfo: result.data,
          })
          if (result.data.awardList && result.data.awardList[0]) {
            that.setAwared(result.data.awardList[0])
          }
          wx.hideLoading()
        }
      })
    },
    setAwared(info: {
      isAward: boolean,
      name: string
      _id: string
    }) {
      this.setData({
        awared: info
      })
      const { userInfo } = this.data
      const baseW = 750/wx.getSystemInfoSync().windowWidth
      setTimeout(() => {
        drawQrcode({
          width: 300 / baseW,
          height: 300 / baseW,
          canvasId: 'myQrcode',
          text: userInfo._id
        })
      }, 100) 
    },
    buy() {
      const { userInfo } = this.data;
      const that = this
      wx.cloud.callFunction({
        name: 'active',
        data: { action: 'buy', params: {  } },
        success({result}: any) {
          if (result.data && result.success) {
            if (result.data.isAward) {
              that.setData({
                isShow: true,
                userInfo: {
                  ...userInfo,
                  isGet: false,
                  hasAward: "in"
                }
              })
              that.setAwared(result.data)
            }  else {
              wx.showToast({
                icon: "none",
                title: result.data.name
              })
              that.setData({
                userInfo: {
                  ...userInfo,
                  isGet: false,
                  hasAward: "off",
                },
              })
            }
          } else {
            wx.showToast({
              icon: "none",
              title: result.errMsg
            })
          }
          console.log(result)
        }
      })
    },
    info() {
      const that = this
      wx.cloud.callFunction({
        name: 'active',
        data: { action: 'info', params: {  } },
        success({result}: any) {
          if (result.data)
          that.setData({
            infoData: result.data
          })
        }
      })
    },
  },
})
